package com.scalar.chiptrack.operations.dataentry.rmaReceive.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.rmaReceive.forms.RMAReceiveForm;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.utils.AutoGrowingArrayList;
import com.scalar.chiptrack.utils.WorkWeek;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;

/**
 * User: ChandraSekhar
 * Date: Aug 22, 2008
 * Time: 4:33:39 PM
 */
public class RMAReceiveLotDetailsCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/rmaReceive.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {


        RMAReceiveForm rmaReceiveForm = (RMAReceiveForm) form;

        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        ArrayList dateCodeList = new ArrayList();
        int currentwk1; int currentwk2; int currentwk3;
        String yearAsDigit1; String yearAsDigit2; String yearAsDigit3;
        WorkWeek wk = WorkWeek.getCurrentWorkWeekInstance();
        currentwk1 = wk.getCurrentWWNumber();
        yearAsDigit1  = (wk.getWorkWeekYear()+"").substring(2);
        if ( currentwk1 == 1 )
        {
            Date dt =  wk.getCurrentWWStartDate();
            Calendar previousWkDate = Calendar.getInstance();
            previousWkDate.setTime( dt );
            previousWkDate.add( Calendar.DATE, -3 );
            WorkWeek prevWk = WorkWeek.getWorkWeekInstance( previousWkDate.getTime() );
            currentwk2 = prevWk.getCurrentWWNumber();
            currentwk3 = currentwk2 -1;
            yearAsDigit2  = (prevWk.getWorkWeekYear()+"").substring(2);
            yearAsDigit3 = yearAsDigit2;
        }
        else if ( currentwk1 == 2 )
        {
            currentwk2 = currentwk1 -1;
            yearAsDigit2  = yearAsDigit1;

            Date dt =  wk.getCurrentWWStartDate();
            Calendar previousWkDate = Calendar.getInstance();
            previousWkDate.setTime( dt );
            previousWkDate.add( Calendar.DATE, -10 );
            WorkWeek prevWk = WorkWeek.getWorkWeekInstance( previousWkDate.getTime() );
            currentwk3 = prevWk.getCurrentWWNumber();
            yearAsDigit3  = (prevWk.getWorkWeekYear()+"").substring(2);

        }
        else
        {
            currentwk2 = currentwk1 -1;
            currentwk3 = currentwk1 -2;
            yearAsDigit2  = yearAsDigit1;
            yearAsDigit3 = yearAsDigit1;
        }

        dateCodeList.add( new LabelValueBean(yearAsDigit3 + (currentwk3<10?"0"+currentwk3 : ""+currentwk3), yearAsDigit3 + (currentwk3<10?"0"+currentwk3 : ""+currentwk3) ) );
        dateCodeList.add( new LabelValueBean(yearAsDigit2 + (currentwk2<10?"0"+currentwk2 : ""+currentwk2), yearAsDigit2 + (currentwk2<10?"0"+currentwk2 : ""+currentwk2) ) );
        dateCodeList.add( new LabelValueBean(yearAsDigit1 + (currentwk1<10?"0"+currentwk1 : ""+currentwk1), yearAsDigit1 + (currentwk1<10?"0"+currentwk1 : ""+currentwk1) ) );

        dateCodeList.add( new LabelValueBean("--other--", "0") );

        ArrayList vendorList = new ArrayList();
        try
        {
            if ( user.getVendorId() > 0 ) // for VENDOR INTERFACE
            {
                if (rmaReceiveForm.getLocationId() == 0 )
                {
                    vendorList = VendorDAO.getInventoryLocationsByProcessByUserType( null, LOCATION_TYPE_FINISHED_GOODS, user.getVendorId() );
                    if ( vendorList != null && vendorList.size() > 0 )
                    {
                        rmaReceiveForm.setLocationId( Integer.parseInt ( ( (LabelValueBean)vendorList.get(0) ).getValue() ) );
                    }
                    else
                    {
                        rmaReceiveForm.setLocationId( -1 );
                    }
                }
            }
           
            if ( rmaReceiveForm.getRmaReceiveId() == -1 )
            {
                //For Setting the Default Bins
                ArrayList ramReceiveLotList = new AutoGrowingArrayList(LotDetails.class);
                ramReceiveLotList.add( new LotDetails() );
                ramReceiveLotList.add( new LotDetails() );
                ramReceiveLotList.add( new LotDetails() );

                rmaReceiveForm.setRmaReceiveLotsList( ramReceiveLotList );
            }
            else
            {
                 ArrayList rmaReceiveLotsList = DataEntryDAO.getAllRMAReceivedLots( rmaReceiveForm.getRmaReceiveId(), 0, 0, 0 );

                 /*rmaReceiveLotsList.add( new LotDetails() );
                 rmaReceiveLotsList.add( new LotDetails() );
                 rmaReceiveLotsList.add( new LotDetails() );*/

                 rmaReceiveForm.setRmaReceiveLotsList( rmaReceiveLotsList );
            }
            request.setAttribute("dateCodeList", dateCodeList );
            request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( LOCATION_TYPE_FG ) );
            request.setAttribute("locationList",    VendorDAO.getInventoryLocationsByProcessByUserTypeWithOutCity( null, LOCATION_TYPE_FINISHED_GOODS, user.getVendorId() ) );

            request.setAttribute("rmaLotsList", DataEntryDAO.getAllRMAReceivedLotsListByCustomer( rmaReceiveForm.getCustomerId(), rmaReceiveForm.getLocationId() ) );
            request.setAttribute("customersList", CustomerDAO.getAllCustomerNamesNoStringsAttached() );
            request.setAttribute("action", action);
       }
       catch( Exception e)
       {
          request.setAttribute("customersList", CustomerDAO.getAllCustomerNamesNoStringsAttached() );
          errors.put( "Unable to get Rma Lot List", null);
          e.printStackTrace();
       }
        return mapping.findForward("rmaReceive");
    }
}